﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinFormExam
{
    public class DbHelper
    {
        public static readonly string conString = "server = .; uid = sa; pwd = 123456; database = Blogs";

        /// <summary>
        /// 获得表数据
        /// </summary>
        /// <param name="sql">Select</param>
        /// <returns></returns>
        public static DataTable GetDataTable(string sql)
        {
            SqlConnection connection = new SqlConnection(conString);

            SqlDataAdapter adapter = new SqlDataAdapter(sql, connection);

            DataTable dt = new DataTable();

            connection.Open();

            adapter.Fill(dt);

            return dt;
            

        }

        internal static object GetDataTable(object sql)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 增加、删除、修改操作
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static int AddOrUpdateOrDelete(string sql)
        {
            SqlConnection connection = new SqlConnection(conString);

            SqlCommand command = new SqlCommand(sql, connection);

            connection.Open();

            return command.ExecuteNonQuery();
        }




        public static  DbDataReader GetDataReader(string uid, string pwd)
        {
            SqlConnection connection = new SqlConnection(conString);

            SqlCommand com = new SqlCommand("select Username from Users where Username ='" + uid + "'", connection);
            SqlDataAdapter da = new SqlDataAdapter(com);
            connection.Open();
            return com.ExecuteReader();
            //DataSet ds = new DataSet();
            
      

        }
    }
}